package com.hebut.chao;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		int x1 = 0x11;
		Log.e("0x11的int类型数据是", ""+x1);
		byte []b1 = intToByteArray(x1);
		Log.e("0x11的byte数组是", ""+byteArrayToInt(b1));
		Log.e("b1的长度是", ""+b1.length);
		
		
	}
	
	public static int byteArray2Int(byte[] bytes) {
        int value= 0;
        //由高位到低位
        for (int i = 0; i < 4; i++) {
            int shift= (4 - 1 - i) * 8;
            value +=(bytes[i] & 0x000000FF) << shift;//往高位游
        }
        return value;
  }
	  public static byte[] int2ByteArray(int i) {   
          byte[] result = new byte[4];   
          //由高位到低位
          result[0] = (byte)((i >> 24) & 0xFF);
          result[1] = (byte)((i >> 16) & 0xFF);
          result[2] = (byte)((i >> 8) & 0xFF); 
          result[3] = (byte)(i & 0xFF);
          return result;
        }

	private byte[] intToByteArray ( int integer) {
		ByteArrayOutputStream boutput = new ByteArrayOutputStream();
		DataOutputStream doutput = new DataOutputStream(boutput);
		try {
			doutput.writeInt(integer);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		byte[] buf = boutput.toByteArray();

		return (buf);
		}
	public static int byteArrayToInt(byte[] b) {
        ByteArrayInputStream bintput = new ByteArrayInputStream(b);
        DataInputStream dintput = new DataInputStream(bintput);
        int i = 0;
		try {
			i = dintput.readInt();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return i;
 }
	
	 
    //网上找的例子，还没有测试
    private static byte[] ShorttoByteArray(short n) {
        byte[] b = new byte[2];
        b[0] = (byte) (n & 0xff);
        b[1] = (byte) (n >> 8 & 0xff);
        return b;
    }
  
    private static byte[] InttoByteArray(int n) {
        byte[] b = new byte[4];
        b[0] = (byte) (n & 0xff);
        b[1] = (byte) (n >> 8 & 0xff);
        b[2] = (byte) (n >> 16 & 0xff);
        b[3] = (byte) (n >> 24 & 0xff);
        return b;
    }
    //以下是学长给的
    /**
	 * 将byte数组变成字符串
	 * 
	 * @param b 待处理字符串
	 * @param len 字符串长度
	 * @return 产生的字符串
	 */
	public static String Bytes2HexString(byte[] b) {
		String ret = "";
		for (int i = 0; i < b.length; i++) {
			String hex = Integer.toHexString(b[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			ret += hex.toUpperCase();
		}
		return ret;
	}
	//时间
	public static long String2TS(String dateStr) {
		long ret = -1;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date;
		try {
			date = sdf.parse(dateStr);
			ret = date.getTime();
			ret = ret / 1000;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ret;
	}
	public static String TS2String(long TS) {
		String time = "";
		Date date = new Date();
		date.setTime(TS * 1000);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		time = sdf.format(date);
		return time;
	}


}
